using UnityEngine;
using System.Collections;

public class drawTiles : MonoBehaviour 
{
	
	public Texture2D [] strip;	
	
	public Texture2D red;
	public int camSize = 21;
	public int tileSize = 32;
	
	
	public Texture2D sheet;
	
	
	public info world;
	
	public bool on = false;
	
	
	
	// Use this for initialization
	void Start () 
	{
		strip = new Texture2D[9];
		int target = 0;
		 Color[] tileColor;
		for(int i = 0;i<3;i++)
		{
			for(int j = 0;j<3;j++)
			{
			 	tileColor = sheet.GetPixels(tileSize*j, tileSize*i, tileSize, tileSize);
				strip[target] = new  Texture2D(tileSize, tileSize, TextureFormat.ARGB32, false);
				strip[target].SetPixels(tileColor);
				strip[target].Apply();
				target++;
			}
			
		}
		
		
	}
	
	// Update is called once per frame
	void Update () 
	{
	
	}
	
	void OnGUI ()
	{
		if(on)
			{
			
		if(Event.current.type.Equals(EventType.Repaint))
				{			
				int xMod =0;
				int yMod = 0;
			if(world.pX>((camSize-1)/2))
						{
						if(world.pX<(world.siX-((camSize-1)/2)))
						   {
							xMod = 	(world.pX-((camSize-1)/2));
							}
						else
						{
						xMod =	(world.siX-camSize);
						}
						}
						
						
			if(world.pY>((camSize-1)/2))
						{
							if(world.pY<(world.siY-((camSize-1)/2)))
						   {
								yMod = 	(world.pY-((camSize-1)/2));
							}
							else
							{
							yMod =	(world.siY-camSize);
							}
						}
						
						
						
							for(int i =0;i<camSize;i++)
									{
												for(int j = 0;j<camSize;j++)
											{
							
												Graphics.DrawTexture(new Rect((i*tileSize),(j*tileSize),tileSize,tileSize),strip[world.grid[i+xMod,j+yMod]]);
											
											}
									}
					if(xMod>0&&xMod != (world.siX-camSize))
						{
							xMod = (camSize-1)/2;
						}
							else
							{
									if(xMod == (world.siX-camSize))
								   {
									xMod = camSize-(world.siX-world.pX);
									}
									else
									{		   
									xMod = world.pX;
									}
							}
					if(yMod>0&&yMod !=(world.siY-camSize))
					{
						yMod = (camSize-1)/2;
					}
					else
							{
									if(yMod == (world.siY-camSize))
								   {
									yMod = camSize-(world.siY-world.pY);
									}
									else
									{		   
									yMod = world.pY;
									}
							}
					
									
									Graphics.DrawTexture(new Rect((xMod*tileSize),(yMod*tileSize),tileSize,tileSize),red);
						
					}	
			
				}
		} 	
}
